<template>
  <div>
    <el-button-group style="width:100%;margin-bottom:10px">
      <section class="right-but" style="display:flex;margin-bottom:0px">
        <el-button type="text"  style="margin-right:-13px;cursor:pointer;" @click="refreshList">
          <img style="width:44px;height:36px;" src="~@/assets/img/shuaxin.png" >
        </el-button>
        <PopoverBut :checkedProps="checkedProps" style="" :options="options" @getCheckeds="handlerCloumn"></PopoverBut>

      </section>
    </el-button-group>
    <el-treetable :data="dataList" isBigData max-height="1200" isTreeTable size="medium" v-loading="loading" row-key="id"
      class="table table-wrapper listTable-wrapper">
      <el-treetable-column prop="name" show-overflow-tooltip align="left" width="200" label="机构名称">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" v-if="hasPermission('sys:office:edit')" @click="edit(scope.row.id)">{{scope.row.name}}</el-link>
          <el-link type="primary" :underline="false" v-else-if="hasPermission('sys:office:view')" @click="view(scope.row.id)">{{scope.row.name}}</el-link>
          <span v-else>{{scope.row.name}}</span>
        </template>
      </el-treetable-column>
      <el-treetable-column prop="code" width="150" label="机构编码">
      </el-treetable-column>
      <el-treetable-column width="200" prop="grade" label="机构类型">
        <template slot-scope="scope">
          {{$dictUtils.getDictLabel("sys_office_grade", scope.row.grade)}}
        </template>
      </el-treetable-column>
      <el-treetable-column prop="useable" label="是否可用">
        <template slot-scope="scope">
          <el-tag :type="scope.row.useable == '0' ? 'danger' : ''">
            {{ $dictUtils.getDictLabel("yes_no", scope.row.useable, '否') }}
          </el-tag>
        </template>
      </el-treetable-column>
      <span v-for="(item,index) in checkedProps" :key="index">
         <el-treetable-column  prop="idCard" :label="item" v-if="item==='身份证号'"></el-treetable-column>
         <el-treetable-column  prop="codes" :label="item" v-if="item==='统一社会代码'"></el-treetable-column>
         <el-treetable-column  prop="updateBy.loginName" :label="item" v-if="item==='创建人'"></el-treetable-column>
         <el-treetable-column  prop="createDate" :label="item" v-if="item==='创建时间'"></el-treetable-column>
         <el-treetable-column  prop="updateBy.loginName" :label="item" v-if="item==='上次操作人'"></el-treetable-column>
         <el-treetable-column  prop="updateDate" :label="item" v-if="item==='上次操作时间'"></el-treetable-column>

      </span>
      <el-treetable-column fixed="right" width="310" label="操作">
        <template slot-scope="scope">
          <el-button v-if="hasPermission('sys:office:view')" type="text" size="small" @click="view(scope.row.id)">详情</el-button>
          <el-button v-if="hasPermission('sys:office:edit')" type="text" size="small" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('sys:office:del')" type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          <el-button v-if="hasPermission('sys:office:add')" type="text" size="small"
            @click="addChild(scope.row.id, scope.row.name)">添加下级机构</el-button>
        </template>
      </el-treetable-column>
    </el-treetable>
    <!-- 弹窗, 新增 / 修改 -->
    <OfficeForm ref="officeForm" @refreshDataList="refreshList"></OfficeForm>
  </div>
</template>

<script>
  import OfficeForm from './OfficeForm'
  import PopoverBut from '@/components/button/popoverBut'
  const options1 = []
  export default {
    data () {
      return {
        checkedProps: options1,
        options: [{label: '创建人', key: '5'}, {label: '创建时间', key: '2'},{label: '上次操作人', key: '6'},{label: '上次操作时间', key: '1'}],
        loading: false,
        dataList: []
      }
    },
    components: {
      OfficeForm,
      PopoverBut
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 是否显示某列数据
      handlerCloumn (e) {
        this.checkedProps = e
        // console.log(e)
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/sys/office/treeData',
          method: 'get'
        }).then(({
          data
        }) => {
          this.dataList = data.treeData
          this.loading = false
        })
      },
      // 新增下级
      addChild (id, name) {
        this.$refs.officeForm.init('addChild', {
          id: '',
          parent: {
            id: id,
            name: name
          }
        })
      },
      // 新增
      add () {
        this.$refs.officeForm.init('add', {
          id: '',
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 修改
      edit (id) {
        this.$refs.officeForm.init('edit', {
          id: id,
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 查看
      view (id) {
        this.$refs.officeForm.init('view', {
          id: id,
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 删除
      del (id) {
        this.$confirm(`确定删除该记录吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/sys/office/delete',
            method: 'delete',
            params: {
              id: id
            }
          }).then(({
            data
          }) => {
            this.loading = false
            if (data && data.success) {
              this.$message({
                message: data.msg,
                type: 'success',
                duration: 1500
              })
              this.refreshList()
            }
          })
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .table-wrapper{
    ::v-deep.el-table{
      th:first-child .cell,td:first-child .cell{
        text-align: left !important;
        padding: 0 20px !important;
      }

    }
  }
</style>
